<!doctype html>
<html lang="en">
<head th:replace="layout::common_head(~{::title}, ~{}, ~{::style})">
    <title>登录</title>
    <style>
        html, body {
            height: 100%;
        }

        body {
            display: flex;
            align-items: center;
            padding-top: 0px;
            padding-bottom: 40px;
            background-color: #fff;
        }

        .logo {
            position: absolute;
            left: 30px;
            top: 30px;
        }

        .form-signin {
            width: 100%;
            max-width: 380px;
            padding: 15px;
            margin: auto;
        }

        .form-signin .checkbox {
            font-weight: 400;
        }

        .form-signin .form-floating:focus-within {
            z-index: 2;
        }

        .form-signin input[type="text"] {
            margin-bottom: -1px;
            border-bottom-right-radius: 0;
            border-bottom-left-radius: 0;
        }

        .form-signin input[type="password"] {
            margin-bottom: 10px;
            border-top-left-radius: 0;
            border-top-right-radius: 0;
        }

        .right {
            position: absolute;
            bottom: 30px;
        }

        @media (min-width: 768px) {
            .bd-placeholder-img-lg {
                font-size: 3.5rem;
            }
        }
    </style>
</head>
<body class="text-center">
<img class="logo mb-4" src="../static/logo.png" alt="" height="60">
<main class="form-signin">
    <h1 class="h1 mb-5 fw-normal text-center d-block clearfix">微服务管理登录</h1>
    <form action="/login" method="post" id="loginForm">
        <div class="form-floating">
            <input type="text" class="form-control" id="username" placeholder="Username" name="username">
            <label for="username">Username</label>
        </div>
        <div class="form-floating">
            <input type="password" class="form-control" id="password" placeholder="Password" name="password">
            <label for="password">Password</label>
        </div>
        <div class="checkbox mb-3">
            <label>
                <input type="checkbox" value="remember-me" id="remember"> Remember me
            </label>
        </div>
        <button class="w-100 btn btn-lg btn-primary" type="submit">Sign in</button>
    </form>
</main>
<p class="right w-100 text-muted">&copy; 2017–2021 Xiamen Nascent Corporation. All rights reserved.</p>
</body>
</html>
<script>
    window.onload = function () {
        var oForm = document.getElementById('loginForm');
        var oUser = document.getElementById('username');
        var oPswd = document.getElementById('password');
        var oRemember = document.getElementById('remember');
        //页面初始化时，如果帐号密码cookie存在则填充
        if (getCookie('username') && getCookie('password')) {
            oUser.value = getCookie('username');
            oPswd.value = getCookie('password');
            oRemember.checked = true;
        }
        //复选框勾选状态发生改变时，如果未勾选则清除cookie
        oRemember.onchange = function () {
            if (!this.checked) {
                delCookie('username');
                delCookie('password');
            }
        };
        //表单提交事件触发时，如果复选框是勾选状态则保存cookie
        oForm.onsubmit = function () {
            if (remember.checked) {
                setCookie('username', oUser.value, 7); //保存帐号到cookie，有效期7天
                setCookie('password', oPswd.value, 7); //保存密码到cookie，有效期7天
            }
        };
    };

    //设置cookie
    function setCookie(name, value, day) {
        var date = new Date();
        date.setDate(date.getDate() + day);
        document.cookie = name + '=' + value + ';expires=' + date;
    };

    //获取cookie
    function getCookie(name) {
        var reg = RegExp(name + '=([^;]+)');
        var arr = document.cookie.match(reg);
        if (arr) {
            return arr[1];
        } else {
            return '';
        }
    };

    //删除cookie
    function delCookie(name) {
        setCookie(name, null, -1);
    };
</script>